home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.05 May 96 / 12.05 Challenge Text next >
Encoding:
Text File  |  1996-04-18  |  4.7 KB  |  80 lines  |  [TEXT/R*ch]

  1. Edge Detector
  2. This month’s Challenge is to write a small image-processing application that
  3. scans a color image and identifies the boundaries of possible objects in that
  4. image.  Applications for such a program might include image enhancement, special
  5. effects, or pattern recognition, although those applications would use a more
  6. sophisticated approach for detecting edges than we will be implementing for this
  7. Challenge.
  8. The prototype for the code you should write is:
  9.  
  10. typedef enum {
  11.     redOnly=1, greenOnly, redAndGreen, blueOnly,
  12.     redAndBlue, greenAndBlue, redGreenAndBlue
  13. } EdgeType;
  14.  
  15. void EdgeDetect(
  16.     PixMapHandle pMapH,                /* find edges in this PixMap */
  17.     BitMap *bMap,                            /* store edges in this BitMap */
  18.     unsigned short threshold,    /* color separations >= dist create an edge */
  19.     EdgeType eType                        /* which color components to look at
  20. */
  21. );
  22. Each pixel in the PixMap should be compared to the eight (or fewer) adjacent
  23. pixels differing in position by up to one row or column.  If the pixel color is
  24. sufficiently different (as defined below) from any of the adjacent pixels, then
  25. the bit in the BitMap corresponding to that pixel should be set to 1. 
  26. Otherwise, the BitMap bit should be set to 0.  Obviously, pixels located in the
  27. first and last row and column will have fewer than eight adjacent pixels.
  28. Whether two pixels differ by enough to constitute an edge is determined by
  29. comparing their rgb values.  The distance between two pixels is the
  30. root-mean-square difference between the color components of their rgb values,
  31. considering only those components specified in the input EdgeType.  For example,
  32. if the EdgeType is greenOnly, then the distance between two pixels is the
  33. absolute value of the difference in the green components of their colors.  If
  34. the EdgeType is redGreenAndBlue, then the distance is the square root of the sum
  35. of the squares of the differences of the red components, the green components,
  36. and the blue components.
  37. As a specific example, suppose we have two pixels with (red, green, blue) values
  38. of (0x1000, 0x2000, 0x4000) and (0x2000, 0x5000, 0xB000).  The distance between
  39. these two pixels is:
  40.  
  41.     redOnly:                    0x1000
  42.     redAndGreen:            0x3298=sqrt(0x01000000+0x09000000)
  43.     redGreenAndBlue:    0x7AE5=sqrt(0x01000000+0x09000000+0x31000000)
  44.  
  45. Two pixels define an edge if their distance is greater than or equal to the
  46. threshold parameter.  The threshold parameter is deliberately declared to be an
  47. unsigned short, even though pixels can differ by a greater amount.  Since the
  48. definition of distance is symmetric, the bits corresponding to both edge pixels
  49. would be set in the BitMap.
  50. The BitMap will be allocated and initialized for you by the calling routine. 
  51. The storage pointed to by the BitMap baseAddr will also be allocated and
  52. initialized to zero.  The bounds rectangles will be the same for the BitMap and
  53. the PixMap.  Your code needs to deal with pixelSize values of 8, 16, or 32, with
  54. each case being equally weighted in the scoring.  For PixMaps with indexed
  55. pixels, you will obviously need to look at the color table to find the rgb value
  56. corresponding to a given index.  In the 16-bit case, you should follow the rules
  57. for converting a 5-bit color component into an 8-bit RGBColor component value
  58. (i.e., replicating the 3 most significant bits and appending them to constitute
  59. the least significant bits of the 8-bit component).
  60. This will be a native PowerPC Challenge, scored using the latest Metrowerks C
  61. compiler.  (No C++ or Pascal this month.)
  62. Entries Due Ten Days Earlier 
  63. Although two issues may seem like a long time to wait for the results of the
  64. Challenge, it has always been a challenge (no pun intended) to complete the
  65. scoring of results in time for publication two issues later.  We have been
  66. searching for a way to allow a little more time for evaluating the entries and
  67. writing the column without introducing any additional delay between publication
  68. of the problem and publication of the solution.  The Challenge mailing list has
  69. allowed us to deliver the Challenge to readers on a predictable schedule
  70. wherever they live, regardless of variations in mailing dates.  We are going to
  71. use the mailing list to advance the due date for Challenge solutions, without
  72. reducing the amount of time available for solving the Challenge.  Starting with
  73. this month’s contest, Challenge entries will be due earlier, on the 1st of the
  74. month printed on the front cover.  We will mail the problem to the mailing list
  75. on the 12th of the preceding month, also about ten days earlier than before.
  76. If you are not already a member of the Challenge mailing list, you can join the
  77. ~300 subscribers from 25 countries already on the list by sending email to
  78. macjordomo@listmail.xplain.com with the line “sub challenge-A YourName” in the
  79. body.
  80.